<template>
<!-- 角色列表 -->
  <div>
        <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图 -->
    <el-card>
        <el-row>
            <el-col>
                <el-button type="primary">添加角色</el-button>
            </el-col>
        </el-row>
        <!-- 角色列表 -->
        <el-table :data="roleList" border stripe>
            <!-- 展开列 -->
             <el-table-column type="expand">
                <template v-slot="scope">
                    <el-row class="vcenter" v-for="(itemone,idxone) in scope.row.children" :key="itemone.id" >
                        <!-- 一级权限 -->
                        <el-col :span="5">
                            <el-tag closable @close="removeRightById(scope.row,itemone.id)">{{itemone.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <el-col :span="19">
                            <!-- 二级权限 -->
                           <el-row v-for="itemtwo in itemone.children" :key="itemtwo.id" :class="['bdbottom',idxone === '0'? 'bdbottom':'','vcenter']">
                                <el-col :span="6" class="vcenter">
                                    <el-tag closable @close="removeRightById(scope.row,itemstwo.id)" type="success">{{itemtwo.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col class="vcenter">
                                <el-tag  closable @close="removeRightById(scope.row,itemsthree.id)" v-for="itemsthree in itemtwo.children" :key="itemsthree.id" type="warning">
                                    {{itemsthree.authName}}
                                 </el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                           </el-row>
                        </el-col>
                    </el-row>
                    <!-- <pre>
                        {{scope.row}}
                    </pre> -->
                </template>
             </el-table-column>
            <!-- 索引列 -->
             <el-table-column type="index"></el-table-column>
             <el-table-column label="角色名称" prop="roleName"></el-table-column>
             <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
             <el-table-column label="操作" prop="roleDesc">
                <template v-slot="scope">
                    <!-- <template> -->
                      <!-- {{scope.row}} -->
                    <el-button type="primary" icon="el-icon-edit" size="mini" >编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
                    <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>
                </template>
             </el-table-column>
        </el-table>
    </el-card>

    <!-- 分配权限的弹出框 -->
    <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        >
        <!-- 树形结构 -->
        <el-tree
        ref="treeRef"
        :data="rightList"
        :props="treeProps"
        show-checkbox
        node-key='id'
        default-expand-all
        :default-checked-keys='defKeys'></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="setRightDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { instance } from '@/utils/http'
export default {
  data () {
    return {
      // 角色的ID
      roleId: '',
      // 所有角色列表数据
      roleList: [],
      rightList: [],
      defKeys: [],
      // 树形控件数据
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      setRightDialogVisible: false
    }
  },
  created () {
    this.getInfoList()
  },
  methods: {
    // ajax
    async getInfoList () {
      const { data: res } = await instance.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.roleList = res.data
    },
    // 删除对应的权限
    async removeRightById (role, rightId) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消成功')
      }
      const { data: res } = await instance.delete('roles/' + role.id + '/rights/' + rightId)
      if (res.meta.status !== 200) {
        return this.$message.error('删除权限失败')
      }
      //   this.getInfoList()
      role.children = res.data
      return this.$message.success('删除权限成功')
    },
    // 展示权限
    async showSetRightDialog (role) {
      this.roleId = role.id
      const { data: res } = await instance.get('rights/tree ')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限失败')
      }
      this.rightList = res.data
      // 调用递归
      this.defKeys = []
      this.getleafKeys(role, this.defKeys)
      this.setRightDialogVisible = true
      // console.log(role)
    },
    // 递归函数
    getleafKeys (node, arr) {
      // console.log(node.id)
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getleafKeys(item, arr)
      })
    },
    async allotRights () {
      const keys = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()]
      const keyStrs = keys.toString(',')
      const { data: res } = await instance.post('roles/' + this.roleId + '/rights', { rids: keyStrs })
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.$message.success('分配权限成功')
      this.getInfoList()
      this.setRightDialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.el-tag{
    margin: 7px;
}
.bdtop{
    border-top: 1px #eee solid;
}
.bdbottom{
    border-bottom: 1px #eee solid;
}
.vcenter{
    display: flex;
    align-items: center;
}
</style>
